var express = require('express');
var router = express.Router();
var fs = require('fs')
var file ='/kwor/source/api/tips.json';


/* GET home page. */
router.get('/', function(req, res, next) {
  var jsonstr = fs.readFileSync(file, 'utf-8');
  res.render('index', { title: 'json编辑工具',editjson: jsonstr });

});

/* GET home page. */
router.post('/save', function(req, res, next) {


  var ejsonstr = req.body.jinfo;//post专用


  fs.writeFile(file,ejsonstr,{flag:'w',encoding:'utf-8',mode:'0666'},function(err){
    if(err){
      res.json({
        status: '2',
        msg: "保存失败"
      })
      console.log("文件写入失败")
    }else{


      res.json({
        status: '1',
        msg: "保存成功"
      })
      console.log("文件写入成功");


    }

  })




});

module.exports = router;
